package cn.frontop.funswitch.controller;

import cn.frontop.funswitch.common.Result;
import cn.frontop.funswitch.entity.SqlScriptEntity;
import cn.frontop.funswitch.service.SqlScriptService;
import cn.frontop.funswitch.util.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * SQL脚本
 *
 * @author Yun
 * @email Yun@frontop.cn
 * @date 2022-11-29 15:46:00
 */
@RestController
@RequestMapping("sqlScript")
@Api(tags = "Sql脚本")
public class SqlScriptController {
    @Autowired
    SqlScriptService sqlScriptService;

    /**
     * 列表
     *
     * @param pageNo   当前页码
     * @param pageSize 每页显示条数
     * @return 结果集
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("列表")
    public Result<?> queryApiPathList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(name = "projectId", defaultValue = "1") Integer projectId) {
        return CommonUtil.entityPageList(sqlScriptService, pageNo, pageSize, new QueryWrapper<SqlScriptEntity>().lambda().eq(SqlScriptEntity::getProjectId, projectId).orderByDesc(SqlScriptEntity::getCreatedTime));
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询信息")
    public Result<SqlScriptEntity> info(@PathVariable("id") Integer id) {
        return Result.ok(sqlScriptService.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存")
    public Result<String> save(@RequestBody SqlScriptEntity sqlScriptEntity) {
        sqlScriptEntity.setUpdatedTime(new Date());
        sqlScriptEntity.setCreatedTime(new Date());
        sqlScriptService.save(sqlScriptEntity);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("更新")
    public Result<String> update(@RequestBody SqlScriptEntity sqlScriptEntity) {
        sqlScriptEntity.setUpdatedTime(new Date());
        sqlScriptService.updateById(sqlScriptEntity);
        return Result.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除")
    public Result<String> delete(@PathVariable("id") Integer id) {
        sqlScriptService.removeById(id);
        return Result.ok();
    }
}
